#include "SW1248.h"
#include <Arduino.h>
#include "log.h"
bool check_platform_is_big_endian()
{
	int a =1 ;
	int b = a << 2;
    return (b==2);
}
void unit_test_usage_for_sw1248()
{
    #if SW1248_TEST_USAGE
    switch_keycode_t sk={0};
    sk.KB_D1 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 0;
    sk.KB_D2 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 1;
    sk.KB_D2 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 0;
    sk.KB_D2 = 0;
    sk.KB_D3 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 1;
    sk.KB_D2 = 0;
    sk.KB_D3 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 0;
    sk.KB_D2 = 1;
    sk.KB_D3 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 1;
    sk.KB_D2 = 1;
    sk.KB_D3 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 0;
    sk.KB_D2 = 0;
    sk.KB_D3 = 0;
    sk.KB_D4 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 1;
    sk.KB_D2 = 0;
    sk.KB_D3 = 0;
    sk.KB_D4 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 0;
    sk.KB_D2 = 1;
    sk.KB_D3 = 0;
    sk.KB_D4 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 1;
    sk.KB_D2 = 1;
    sk.KB_D3 = 0;
    sk.KB_D4 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 0;
    sk.KB_D2 = 0;
    sk.KB_D3 = 1;
    sk.KB_D4 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 1;
    sk.KB_D2 = 0;
    sk.KB_D3 = 1;
    sk.KB_D4 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 0;
    sk.KB_D2 = 1;
    sk.KB_D3 = 1;
    sk.KB_D4 = 1;
    Serial.println(sk.value);
    sk.KB_D1 = 1;
    sk.KB_D2 = 1;
    sk.KB_D3 = 1;
    sk.KB_D4 = 1;
    Serial.println(sk.value);
    #endif
}

SW1248::SW1248(uint8_t pin_kb_d1,uint8_t pin_kb_d2,uint8_t pin_kb_d3,uint8_t pin_kb_d4):
    _pin_kb_d1(pin_kb_d1),_pin_kb_d2(pin_kb_d2),_pin_kb_d3(pin_kb_d3),_pin_kb_d4(pin_kb_d4)
{
  pinMode(_pin_kb_d3,INPUT);
  pinMode(_pin_kb_d4,INPUT);

  _sw_last_state.value = 0;
  _sw_read_state.value = 0;

  _counter = 0;
  //_address_num = 0;
  _last_data = 0;
  _last_millis = 0;
  #if SW1248_USE_TIMER
  _update_flag =true;
  #endif
}
SW1248::~SW1248()
{

}
void SW1248::begin()
{
    _sw_last_state.value = read().value;
}
switch_keycode_t& SW1248::state()
{
    return _sw_last_state;
}
switch_keycode_t& SW1248::read()
{
    _sw_read_state.KB_D1 = analogRead(_pin_kb_d1)>500?1:0;
    _sw_read_state.KB_D2 = analogRead(_pin_kb_d2)>500?1:0;
    _sw_read_state.KB_D3 = digitalRead(_pin_kb_d3);
    _sw_read_state.KB_D4 = digitalRead(_pin_kb_d4);
    return _sw_read_state;
}
void SW1248::update()
{   
    #if SW1248_USE_TIMER
    _update_flag = true;
    #endif
}
void SW1248::schedule()
{
    #if SW1248_USE_TIMER
    if(!_update_flag){
         return;
    }
    #else
    if(++_counter != 0xFFF){
        return;
    }
    #endif

    _counter = 0;
    

    if(_sw_last_state.value!=read().value){
        _cb.push(_sw_last_state.value);
        _sw_last_state.value = _sw_read_state.value;
    }

    #if SW1248_USE_TIMER
    _update_flag = false;
    #endif
    //log::debug("sw state:0x%x\r\n",_sw_read_state.value);
}

void SW1248::cleanup()
{
    _cb.reset();
}

bool SW1248::changed()
{
    if(_cb.size()!=0){
        uint8_t data = 0;
        if(_cb.peek(data)){}

        if(data!=_last_data){
            _last_data = data;
            _last_millis = millis();
        }
        return true;
    }
    return false;
}
unsigned long SW1248::last_millis()
{
    return _last_millis;
}
uint8_t SW1248::poll()
{
    uint8_t addr=_sw_last_state.value;

    while(_cb.pop(addr)){}

    //_address_num = 0;
    return addr;
}